//
//  MHHomeViewController.m
//  MHO
//
//  Created by papo on 2/16/14.
//  Copyright (c) 2014 maihoang. All rights reserved.
//

#import "MHHomeViewController.h"
#import "MHAppDelegate.h"
#import "MHDownloadViewController.h"
#import "Account.h"
#import "Lib.h"

@interface MHHomeViewController ()
{
    NSArray* viewControllers;
    NSArray* tabBarImages;
    NSInteger        currentIndex;
}

@end

@implementation MHHomeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    tabBarImages = @[@"tong_hop_focus.png", @"chuyen_de_focus.png", @"khac_focus.png", @"xep_hang_focus.png", @"the_loai_focus.png"];
    
    
    
    UIViewController* vc1 = [[MHTongHopViewController alloc] initWithNibName:@"MHTongHopViewController" bundle:nil];
    vc1 = [[UINavigationController alloc] initWithRootViewController:vc1];
    UIViewController* vc2 = [[MHChuyenDeViewController alloc] initWithNibName:@"MHChuyenDeViewController" bundle:nil];

    vc2 = [[UINavigationController alloc] initWithRootViewController:vc2];
    UIViewController* vc3 = [[MHKhacViewController alloc] initWithNibName:@"MHKhacViewController" bundle:nil];
    vc3 = [[UINavigationController alloc] initWithRootViewController:vc3];
    UIViewController* vc4 = [[MHXepHangViewController alloc] initWithNibName:@"MHXepHangViewController" bundle:nil];
    vc4 = [[UINavigationController alloc] initWithRootViewController:vc4];
    UIViewController* vc5 = [[MHTheLoaiViewController alloc] initWithNibName:@"MHTheLoaiViewController" bundle:nil];
    vc5 = [[UINavigationController alloc] initWithRootViewController:vc5];
    
    viewControllers = @[vc1, vc2, vc3, vc4, vc5];
    for (UIViewController* vc in viewControllers) {
        if (vc) {
            [vc.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - (self.topView.frame.size.height + tabBar.frame.size.height))];
            ((UINavigationController*)vc).navigationBarHidden = YES;
//            [vc.view setTag:1234];
//            UINavigationController* na = (UINavigationController*)vc;
//            na set
            [self addChildViewController:vc];
        }
    }
    currentIndex = 0;
    [self showTabBarAtIndex:1];
    
    NSString* email = [Lib stringForKey:@"Email"];
    NSString* password = [Lib stringForKey:@"paccount"];
    
    if (strCheckNull(email).length > 0
        && strCheckNull(password).length > 0) {
        NSDictionary* dict = @{kAccountEmail:email, kAccountPassword: password };
        [Lib showLoadingView];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            ServiceResult* result = [Account loginWithInfo:dict];
            if (result) {};
            dispatch_async(dispatch_get_main_queue(), ^{
                [Lib removeLoadingView];
            });
        });
    }
    
    
//    self.visibleView.layer.borderColor = [UIColor redColor].CGColor;
//    self.visibleView.layer.borderWidth = 2;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)tabBarButtonTap:(UIButton *)sender {
    [self showTabBarAtIndex:sender.tag];
}

- (void)showTabBarAtIndex:(NSInteger )index {
    NSAssert(index < 6, @"index must < 6");
    UIView* view = [self.visibleView viewWithTag:1234];
    if (view) {
        view.tag = 0;
        [view removeFromSuperview];
    }
    
    // set deselected current button
    UIButton* button = (UIButton*)[tabBar viewWithTag:currentIndex];
    if (button && [button respondsToSelector:@selector(setSelected:)]) {
        [button setSelected:NO];
    }
    
    // set new button selected
    button = (UIButton*)[tabBar viewWithTag:index];
    if (button && [button respondsToSelector:@selector(setSelected:)]) {
        [button setSelected:YES];
    }


    UIView* vc = [viewControllers[index - 1] view];
    [self.visibleView addSubview:vc];
    [vc setTag:1234];
    [vc setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - (/*self.topView.frame.size.height*/ + tabBar.frame.size.height))];
    [self.view bringSubviewToFront:tabBar];

    currentIndex = index;
}

- (IBAction)userButtonPressed:(id)sender {
    [[MHAppDelegate sharedApp].viewController showLeftPanelAnimated:YES];
}

- (IBAction)searchButtonPressed:(id)sender {
    
}

- (IBAction)updateButtonPressed:(id)sender {
    [[MHAppDelegate sharedApp].viewController showDownloadVc];
}


@end
